from flask import render_template,redirect,url_for,session
from flask_login import login_user, login_required, logout_user, current_user

from model.company import CompanyDao
from service.item import ItemService
from service.company import CompanyService
#from model.item import Item
#from dao.picture import PictureDao
#from dao.category import CategoryDao
#from flask_babel import Babel, gettext
from service.post import PostService
from controller import app

@app.route('/')
@app.route('/', methods=['GET','POST'])
def index():
    '''When a user logs in, their session is marked as 'fresh'.
    '''
    company_serv = CompanyService()
    _companies = company_serv.get_companies()
    
    
    if _companies != []:
        item_serv = ItemService()
        _items = item_serv.get_items_by_company(_companies[0])
    else:
        _items = []
       
    if '_fresh' in session.keys() and session['_fresh']:
        '''
        # Prepare categories
        dao = CategoryDao()
        cats = dao.all()
        _categories = []
        for category in cats:
            _categories.append(category.name)
        '''
        return render_template('index.html', companies=_companies, items=_items, username=current_user.username)
    else:
        return render_template("index.html", companies=_companies, items=_items, username=None)


@app.route("/main_dashboard")
@login_required
def main_dashboard():
    user = {'name':current_user.username, 'role':int(current_user.role)}
    service = PostService()
    posts = service.get_post_list()
    return render_template("main_dashboard.html", user=user, errors_json=[], posts=posts)

"""


@app.route("/manage_properties")
@login_required
def manage_properties():
    #item_dao = ItemDao()
    #items = item_dao.filter_by(Item.created, user_id=current_user.id)
    return render_template("manage_properties.html", properties=None, username=current_user.username)



@app.route("/pictures")
@login_required
def pictures():
    picture_dao = PictureDao()
    pics = picture_dao.filter_by(item_id=current_user.id)
    return render_template("pictures.html", pictures=pics)


@app.route("/testing")
@login_required
def testing():
    return render_template("testing.html")
"""